<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="AVL树 平衡因子 (Balance Factor) 某节点的左右树的高度差 每页节点的平衡因子只可能是,1,0,-1(绝对值 &amp;lt;= 1,如果超过1 称之为 失衡) 搜索,添加,删除的时间复杂度是O(logn)    LL - 右旋转 (单旋) LL left left G的left 的 left 中添加元素所以失衡   RR - 左旋转 (单旋) RR Right Right G的Righ">
<meta property="og:type" content="article">
<meta property="og:title" content="AVLTree">
<meta property="og:url" content="http://yoursite.com/2019/10/14/AVLTree/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="AVL树 平衡因子 (Balance Factor) 某节点的左右树的高度差 每页节点的平衡因子只可能是,1,0,-1(绝对值 &amp;lt;= 1,如果超过1 称之为 失衡) 搜索,添加,删除的时间复杂度是O(logn)    LL - 右旋转 (单旋) LL left left G的left 的 left 中添加元素所以失衡   RR - 左旋转 (单旋) RR Right Right G的Righ">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071252.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071451.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-080907.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-081423.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-082350.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-082802.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-15-025017.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-16-015707.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024052.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024239.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024410.png">
<meta property="og:updated_time" content="2019-10-16T03:05:47.615Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="AVLTree">
<meta name="twitter:description" content="AVL树 平衡因子 (Balance Factor) 某节点的左右树的高度差 每页节点的平衡因子只可能是,1,0,-1(绝对值 &amp;lt;= 1,如果超过1 称之为 失衡) 搜索,添加,删除的时间复杂度是O(logn)    LL - 右旋转 (单旋) LL left left G的left 的 left 中添加元素所以失衡   RR - 左旋转 (单旋) RR Right Right G的Righ">
<meta name="twitter:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071252.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/10/14/AVLTree/"/>





  <title>AVLTree | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/14/AVLTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">AVLTree</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-14T15:07:44+08:00">
                2019-10-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/14/AVLTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/14/AVLTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="AVL树"><a href="#AVL树" class="headerlink" title="AVL树"></a>AVL树</h3><blockquote>
<p><strong>平衡因子 (Balance Factor) 某节点的左右树的高度差</strong></p>
<p><strong>每页节点的平衡因子只可能是,1,0,-1(绝对值 &lt;= 1,如果超过1 称之为 失衡)</strong></p>
<p><strong>搜索,添加,删除的时间复杂度是O(logn)</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071252.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-071451.png" alt=""></p>
<h3 id="LL-右旋转-单旋"><a href="#LL-右旋转-单旋" class="headerlink" title="LL - 右旋转 (单旋)"></a>LL - 右旋转 (单旋)</h3><blockquote>
<p><strong>LL left left G的left 的 left 中添加元素所以失衡</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-080907.png" alt=""></p>
<h3 id="RR-左旋转-单旋"><a href="#RR-左旋转-单旋" class="headerlink" title="RR - 左旋转 (单旋)"></a>RR - 左旋转 (单旋)</h3><blockquote>
<p><strong>RR Right Right G的Right 的 Right 中添加元素发生了失衡</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-081423.png" alt=""></p>
<h3 id="LR-RR左旋转-LL右旋转-双旋"><a href="#LR-RR左旋转-LL右旋转-双旋" class="headerlink" title="LR - RR左旋转, LL右旋转 (双旋)"></a>LR - RR左旋转, LL右旋转 (双旋)</h3><blockquote>
<p><strong>LR Left Right G的Left 的 Right 中添加元素发生了失衡,先左旋转,再右旋转</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-082350.png" alt=""></p>
<h3 id="RL-LL右旋转-RR左旋转-双旋"><a href="#RL-LL右旋转-RR左旋转-双旋" class="headerlink" title="RL - LL右旋转, RR左旋转 (双旋)"></a>RL - LL右旋转, RR左旋转 (双旋)</h3><blockquote>
<p><strong>RL Right Left G的Right 的 Left 中添加元素发生了失衡,先右旋转,再左旋转</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-082802.png" alt=""></p>
<p><strong>利用高度判断节点是否失衡 - 左子树的高度 - 右子树的高度</strong></p>
<h3 id="添加-平衡调整"><a href="#添加-平衡调整" class="headerlink" title="添加-平衡调整"></a>添加-平衡调整</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	伪代码 获取平衡因子</span></span><br><span class="line"><span class="comment">//	查验平衡</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">makeBalance</span><span class="params">(Node &lt;E&gt; node)</span></span>&#123;</span><br><span class="line">	<span class="keyword">while</span>((node = node.parent) != <span class="keyword">null</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(isBalancefactor(node))&#123;</span><br><span class="line">      <span class="comment">//	更新高度</span></span><br><span class="line">      updateHeight(node);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">      <span class="comment">//	恢复平衡 这里的node其实是图上的G</span></span><br><span class="line">      rebalance(node);</span><br><span class="line">      <span class="keyword">break</span>;		<span class="comment">//	一旦找到不平衡的直接break</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	获取平衡因子</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">balancefactor</span><span class="params">(Node&lt;&gt; node)</span></span>&#123;</span><br><span class="line"> 	<span class="keyword">int</span> letHeight = node.left == <span class="keyword">null</span> ? <span class="number">0</span> : node.left.height;</span><br><span class="line"> 	<span class="keyword">int</span> rightHeight = node.right == <span class="keyword">null</span> ? <span class="number">0</span> : node.right.height;</span><br><span class="line"> 	<span class="keyword">return</span> leftHeight - rightHeight</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	是否 失去平衡</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isBalancefactor</span><span class="params">(Node&lt;&gt; node)</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> Math.abs(node.balancefactor()) &lt;= <span class="number">1</span>;		<span class="comment">// 小于等于1意味着是平衡因子是 平衡的</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	更新高度 左右子树高度的最大值 加上 1 ,因为是新添加了一个叶子节点么</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">updateHeight</span><span class="params">(Node&lt;E&gt; node)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> letHeight = node.left == <span class="keyword">null</span> ? <span class="number">0</span> : node.left.height;</span><br><span class="line"> 	<span class="keyword">int</span> rightHeight = node.right == <span class="keyword">null</span> ? <span class="number">0</span> : node.right.height;</span><br><span class="line">  node.height = <span class="number">1</span> + (leftHeight &gt; rightHeight ? leftHeight : rightHeight);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	恢复平衡 这里传过来就就图上的G, 高度最低的不平衡的节点</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rebalance</span><span class="params">(Node&lt;E&gt; grand)</span></span>&#123;</span><br><span class="line">  <span class="comment">//	现在g已经有了,要求 p 和 n 图上的!!!</span></span><br><span class="line">  <span class="comment">//	图中发现 p 其实是 g的左右子树高度最高的节点,n是p的左右子树高度最高的节点</span></span><br><span class="line">  	Node&lt;E&gt; parent = grand.tallerChild();		<span class="comment">//	P  tallerChild 返回最高节点方法</span></span><br><span class="line">    Node&lt;E&gt; node = parent.tallerChild();		<span class="comment">//	N</span></span><br><span class="line">  </span><br><span class="line">  <span class="keyword">if</span>(parent.isLeftChild())&#123;		<span class="comment">// L</span></span><br><span class="line">    <span class="keyword">if</span> (node.isLeftChild)&#123;		<span class="comment">// LL</span></span><br><span class="line">				rotateRight(grand);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;										<span class="comment">// LR</span></span><br><span class="line">      rotateLeft(parent);</span><br><span class="line">      rotateRight(grand);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;											<span class="comment">//	R</span></span><br><span class="line">    <span class="keyword">if</span> (node.isLeftChild)&#123;		<span class="comment">// RL</span></span><br><span class="line">      rotateRight(parent);</span><br><span class="line">      rotateLeft(grand);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;										<span class="comment">// RR</span></span><br><span class="line">      rotateLeft(grand);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	左旋 - 看上面的图学就可以了RR </span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotateLeft</span><span class="params">(Node&lt;E&gt; g)</span></span>&#123;</span><br><span class="line">	Node parent = g.right;</span><br><span class="line">  Node t1 = parent.left;</span><br><span class="line">  g.right = t1;</span><br><span class="line">  parent.left = g;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	注意!!! T0到T3都有可能空</span></span><br><span class="line">	<span class="keyword">if</span> (t1 != <span class="keyword">null</span>)&#123;</span><br><span class="line">    	t1.parent = g;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">	g.parent = parent;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	父节点调整</span></span><br><span class="line">  parent.parent = g.parent;</span><br><span class="line">  <span class="keyword">if</span> (g.isLeftChild())&#123;</span><br><span class="line">    g.parent.left = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (g.isRightChild())&#123;</span><br><span class="line">    g.parent.right = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="comment">//	意味着 g是根节点</span></span><br><span class="line">    root = parent;</span><br><span class="line">	&#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	更新高度 先更新G的高度 再更新P的高度 因为G比较矮</span></span><br><span class="line">  updateHeight(g);</span><br><span class="line">  updateHeight(parent);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//	右旋 LL </span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotateRight</span><span class="params">(Node&lt;E&gt; g)</span></span>&#123;</span><br><span class="line">  Node parent = g.lelft;</span><br><span class="line">  Node t2 = parent.right</span><br><span class="line">  g.left = t2;</span><br><span class="line">  parent.right = g;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	注意!!! T0到T3都有可能空</span></span><br><span class="line">	<span class="keyword">if</span> (t2 != <span class="keyword">null</span>)&#123;</span><br><span class="line">    	t2.parent = g;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  g.parent = parent;</span><br><span class="line">  <span class="comment">//	父节点调整</span></span><br><span class="line">  parent.parent = g.parent;</span><br><span class="line">  <span class="keyword">if</span> (g.isLeftChild())&#123;</span><br><span class="line">    g.parent.left = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (g.isRightChild())&#123;</span><br><span class="line">    g.parent.right = parent;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="comment">//	意味着 g是根节点</span></span><br><span class="line">    root = parent;</span><br><span class="line">	&#125;</span><br><span class="line">  </span><br><span class="line">   <span class="comment">//	更新高度 先更新G的高度 再更新P的高度 因为G比较矮</span></span><br><span class="line">  updateHeight(g);</span><br><span class="line">  updateHeight(parent);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	返回最高节点方法</span></span><br><span class="line"><span class="function">Node&lt;e&gt; <span class="title">tallerChild</span><span class="params">(Node&lt;E&gt; node)</span></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> letHeight = node.left == <span class="keyword">null</span> ? <span class="number">0</span> : node.left.height;</span><br><span class="line"> 	<span class="keyword">int</span> rightHeight = node.right == <span class="keyword">null</span> ? <span class="number">0</span> : node.right.height;</span><br><span class="line">	<span class="keyword">if</span> (leftHeight &gt; rightHeight) <span class="keyword">return</span> node.left;</span><br><span class="line">	<span class="keyword">if</span> (leftHeight &lt; rightHeight) <span class="keyword">return</span> node.right;</span><br><span class="line">  <span class="comment">//	相等</span></span><br><span class="line">  <span class="keyword">return</span> node.isLeftChild() ? node.left : node.right;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//	自己是父节点的左节点</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isLeftChild</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> parent != <span class="keyword">null</span> &amp;&amp; <span class="keyword">this</span> == parent.left;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	自己是父节点的右节点</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isRightChild</span><span class="params">()</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> parent != <span class="keyword">null</span> &amp;&amp; <span class="keyword">this</span> == parent.right;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="统一所有旋转操作"><a href="#统一所有旋转操作" class="headerlink" title="统一所有旋转操作"></a>统一所有旋转操作</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-15-025017.png" alt=""></p>
<blockquote>
<p><strong>从上面的图中发现,不管是LL,还是RR,LR,RL,最终旋转后他们变成了一样</strong></p>
<p><strong>a b c 是自组, d是root,e f g是一组</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">rotate</span> <span class="params">(Node&lt;E&gt; root 之前的跟节点,</span></span></span><br><span class="line"><span class="function"><span class="params">						 Node&lt;E&gt; a,Node&lt;E&gt; b, Node&lt;E&gt; c,</span></span></span><br><span class="line"><span class="function"><span class="params">						 Node&lt;E&gt; e,Node&lt;E&gt; f, Node&lt;E&gt; g,)</span></span>&#123;</span><br><span class="line">  <span class="comment">//	让d成为这棵树子树的根节点</span></span><br><span class="line">	d.parent = r.parent;</span><br><span class="line">  <span class="keyword">if</span> (r.isLeftChild(),意思r是它跟节点的左边)&#123;</span><br><span class="line">    r.parent.left = d;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (r.isRightChild(),意思r是它跟节点的右边)&#123;</span><br><span class="line">    r.parent.right = d;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    root = d;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	a - b - c</span></span><br><span class="line">  b.left = a;</span><br><span class="line">  <span class="keyword">if</span> (a != <span class="keyword">null</span>)&#123;</span><br><span class="line">    a.parent = b;</span><br><span class="line">  &#125;</span><br><span class="line">   b.right = c;</span><br><span class="line">  <span class="keyword">if</span> (c != <span class="keyword">null</span>)&#123;</span><br><span class="line">    c.parent = b;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//	更新b的高度</span></span><br><span class="line">  updateHeight(b);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	e - f - g</span></span><br><span class="line">  f.left = e;</span><br><span class="line">  <span class="keyword">if</span> (a != <span class="keyword">null</span>)&#123;</span><br><span class="line">    e.parent = f;</span><br><span class="line">  &#125;</span><br><span class="line">   f.right = g;</span><br><span class="line">  <span class="keyword">if</span> (g != <span class="keyword">null</span>)&#123;</span><br><span class="line">    g.parent = f;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//	更新f的高度</span></span><br><span class="line">  updateHeight(f);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	b - d - f</span></span><br><span class="line">  d.left = b;</span><br><span class="line">  d.right = f;</span><br><span class="line">  </span><br><span class="line">  b.parent = d;</span><br><span class="line">  f.parent = d;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//	更新d的高度</span></span><br><span class="line">  updateHeight(d);</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="删除-调整平衡"><a href="#删除-调整平衡" class="headerlink" title="删除-调整平衡"></a>删除-调整平衡</h3><blockquote>
<p><strong>删除16,15就会失去平衡,删除子节点 只可能会导致父节点失衡,其他节点不可能失衡</strong></p>
<p><strong>删除导致失衡,说明删除的那个节点绝对是相对短的节点,所以不会影响父节点的高度,所以上面的祖先节点都不会有变化</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-015707.png" alt=""></p>
<p><strong>LL - 右旋转 (单旋)</strong></p>
<blockquote>
<p><strong>红色是删除的节点,绿色有可能不存在的节点</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024052.png" alt=""></p>
<blockquote>
<p><strong>删除之后G失去平衡</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024239.png" alt=""></p>
<blockquote>
<p><strong>调整右旋转之后</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-16-024410.png" alt=""></p>
<blockquote>
<p><strong>G恢复了平衡,但是由于T0的高度减少了,那么P的高都是T2,那么如果绿色的部分不存在,那么P的高度比之前G的高度是-1的,所以P的祖先节点也有可能失衡,所以不断的查询祖先节点是否失衡,所以删除while循环的Break,复杂度O(longn),最多调整logn次</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	伪代码 获取平衡因子</span></span><br><span class="line"><span class="comment">//	查验平衡   删除跟添加一样 不同的地方时break删掉就可以了</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">makeBalance</span><span class="params">(Node &lt;E&gt; node)</span></span>&#123;</span><br><span class="line">	<span class="keyword">while</span>((node = node.parent) != <span class="keyword">null</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(isBalancefactor(node))&#123;</span><br><span class="line">      <span class="comment">//	更新高度</span></span><br><span class="line">      updateHeight(node);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">      <span class="comment">//	恢复平衡 这里的node其实是图上的G</span></span><br><span class="line">      rebalance(node);</span><br><span class="line">      <span class="comment">//break;		//	一旦找到不平衡的直接break</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><blockquote>
<p><strong>添加</strong></p>
<p><strong>可能会导致 所有 祖先节点 都失衡</strong></p>
<p><strong>但是只要让高度最低的失衡节点回复平衡,整棵树就恢复平衡(仅需O(1)次调整)</strong></p>
<p><strong>删除</strong></p>
<p><strong>只可能会导致父节点失衡</strong></p>
<p><strong>但是让父节点恢复平衡后,可能会导致更高层的祖先节点失衡,(最多需要O(logn)次调整)</strong></p>
<p><strong>平均时间复杂度</strong></p>
<p><strong>搜索:    O(logn)</strong></p>
<p><strong>添加:    O(logn),    仅需O(1)次的旋转操作</strong></p>
<p><strong>删除:    O(logn),    最多需要O(logn)次的旋转操作</strong>    </p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/10/12/SearchBinaryTree/" rel="next" title="BinarySearchTree">
                <i class="fa fa-chevron-left"></i> BinarySearchTree
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/10/16/RedBlackTree/" rel="prev" title="RedBlackTree">
                RedBlackTree <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#AVL树"><span class="nav-number">1.</span> <span class="nav-text">AVL树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#LL-右旋转-单旋"><span class="nav-number">2.</span> <span class="nav-text">LL - 右旋转 (单旋)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#RR-左旋转-单旋"><span class="nav-number">3.</span> <span class="nav-text">RR - 左旋转 (单旋)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#LR-RR左旋转-LL右旋转-双旋"><span class="nav-number">4.</span> <span class="nav-text">LR - RR左旋转, LL右旋转 (双旋)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#RL-LL右旋转-RR左旋转-双旋"><span class="nav-number">5.</span> <span class="nav-text">RL - LL右旋转, RR左旋转 (双旋)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#添加-平衡调整"><span class="nav-number">6.</span> <span class="nav-text">添加-平衡调整</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#统一所有旋转操作"><span class="nav-number">7.</span> <span class="nav-text">统一所有旋转操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除-调整平衡"><span class="nav-number">8.</span> <span class="nav-text">删除-调整平衡</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#总结"><span class="nav-number">8.1.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
